#include <iostream>
#include <vector>

using namespace std;
struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x)
        :val(x)
        ,left(nullptr)
        ,right(nullptr)
    {}
};
vector<int> ret;
void dfs(TreeNode* root)
{
    if (root == nullptr)
    {
        return;
    }
    dfs(root->left);
    ret.push_back(root->val);
    dfs(root->right);
}
bool isValidBST(TreeNode* root)
{
    dfs(root);
    for (int i = 1; i < ret.size(); i++)
    {
        if (ret[i - 1] > ret[i])
        {
            return false;
        }
    }
    return true;
}